package sing.core.ext.view

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import jp.wasabeef.glide.transformations.BlurTransformation
import sing.core.R


// 加载普通图片
fun ImageView.load(path:String,placeholderResId:Int = -1) {
    val requestOptions = RequestOptions().centerCrop()
    if (placeholderResId == -1){
        val placeholder = drawable?:ContextCompat.getDrawable(context, R.drawable.ic_default_img)
        Glide.with(this.context).load(path).apply(requestOptions).placeholder(placeholder).thumbnail(0.2F).into(this)
    }else{
        Glide.with(this.context).load(path).apply(requestOptions).placeholder(placeholderResId).thumbnail(0.2F).into(this)
    }
}

// 加载普通图片
fun ImageView.loadUri(path: Uri, placeholderResId:Int = -1) {
    val requestOptions = RequestOptions().centerCrop()
    if (placeholderResId == -1){
        val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
        Glide.with(this.context).load(path).apply(requestOptions).placeholder(placeholder).thumbnail(0.2F).into(this)
    }else{
        Glide.with(this.context).load(path).apply(requestOptions).placeholder(placeholderResId).thumbnail(0.2F).into(this)
    }
}
// 加载图片、圆角
fun ImageView.loadRoundUri(uri: Uri, round: Int = 12) {
    val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
    Glide.with(this.context).load(uri)
        .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round))))
        .placeholder(placeholder)
        .thumbnail(Glide.with(this.context).load(uri).apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round)))))
        .into(this)
}


// 加载Bitmap图片
fun ImageView.loadBitmap(bitmap: Bitmap, round: Int = 0) {
    val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
    val requestOptions = if (round == 0) {
        RequestOptions.bitmapTransform(MultiTransformation(CenterCrop()))
    } else {
        RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(), RoundedCorners(round)))
    }

    Glide.with(this.context).load(bitmap)
        .apply(requestOptions)
        .placeholder(placeholder).thumbnail(0.2F).into(this)
}

// 加载Drawable图片
fun ImageView.loadDrawable(drawable: Drawable) {
    Glide.with(this.context).load(drawable)
        .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop())))
        .thumbnail(0.2F).into(this)
}

// 加载本地普通图片
fun ImageView.load(@DrawableRes resId: Int, errorResId:Int = -1) {
    if (errorResId == -1){
        val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
        Glide.with(this.context).load(resId).thumbnail(0.2F).error(placeholder).into(this)
    }else{
        Glide.with(this.context).load(resId).thumbnail(0.2F).error(errorResId).into(this)
    }
}

// 加载 ByteArray，即 base64
fun ImageView.loadByte(byte:ByteArray?,placeholderResId:Int = -1) {
    Glide.with(this.context).load(byte).thumbnail(0.2F).into(this)
}

// 加载 ByteArray，即 base64
fun ImageView.loadRoundByte(byte:ByteArray?, round: Int = 12) {
    Glide.with(this.context).load(byte).thumbnail(0.2F)
        .apply(RequestOptions.bitmapTransform(MultiTransformation(RoundedCorners(round))))
        .into(this)
}

// 加载图片、圆形
fun ImageView.loadCirclePath(path:String,placeholderResId:Int? = -1) {
    if (placeholderResId == null || placeholderResId == -1){
        val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img_circle)
        Glide.with(this.context).load(path)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .placeholder(placeholder)
            .thumbnail(0.2F)
            .into(this)
    }else{
        Glide.with(this.context).load(path)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .placeholder(placeholderResId)
            .thumbnail(0.2F)
            .into(this)
    }
}

// 加载图片、圆形
fun ImageView.loadCirclePath(@DrawableRes resId: Int) {
    Glide.with(this.context).load(resId)
        .apply(RequestOptions.bitmapTransform(CircleCrop()))
        .into(this)
}

// 加载图片、圆形
fun ImageView.loadCircle(bitmap: Bitmap) {
    Glide.with(this.context).load(bitmap)
        .apply(RequestOptions.bitmapTransform(CircleCrop()))
        .thumbnail(0.2F)
        .into(this)
}

// 加载图片、圆角
fun ImageView.loadRoundUrl(url: String, round: Int = 12) {
    val placeholder = ContextCompat.getDrawable(context,R.drawable.ic_default_img)
    Glide.with(this.context).load(url)
        .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round))))
        .placeholder(placeholder)
        .thumbnail(Glide.with(this.context).load(url).apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round)))))
        .into(this)
}

// 加载图片、圆角
fun ImageView.loadRoundUrl(url: String, round: Int = 12,placeholder :Int) {
    val placeholder = drawable?:ContextCompat.getDrawable(context,placeholder)
    if (url.isEmpty()){
        Glide.with(this.context).load(placeholder)
            .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round))))
            .placeholder(placeholder)
            .thumbnail(Glide.with(this.context).load(placeholder).apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round)))))
            .into(this)
        return
    }
    Glide.with(this.context).load(url)
        .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round))))
        .placeholder(placeholder)
        .thumbnail(Glide.with(this.context).load(url).apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round)))))
        .into(this)
}



// 视频瀑布流专属
fun ImageView.loadVideoRoundUrl(url: String, round: Int = 12,placeholder :Int) {
    val placeholder = ContextCompat.getDrawable(context,placeholder)
    if (url.isEmpty()){
        Glide.with(this.context).load(placeholder)
            .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round))))
            .placeholder(placeholder)
            .thumbnail(Glide.with(this.context).load(placeholder).apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round)))))
            .into(this)
        return
    }
    Glide.with(this.context).load(url)
        .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round))))
        .placeholder(placeholder)
        .thumbnail(Glide.with(this.context).load(url).apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round)))))
        .into(this)
}

// 加载图片、圆角
fun ImageView.loadRoundUrl(@DrawableRes resId: Int, round: Int = 12) {
    val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
    Glide.with(this.context).load(resId)
        .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round))))
        .placeholder(placeholder)
        .thumbnail(0.2F)
        .into(this)
}

// 加载图片、圆角
fun ImageView.loadRoundBitmap(bitmap: Bitmap, round: Int = 12) {
    val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
    Glide.with(this.context).load(bitmap)
        .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),RoundedCorners(round))))
        .placeholder(placeholder)
        .thumbnail(0.2F)
        .into(this)
}

// 加载图片、指定圆角
fun ImageView.loadRoundUrl(url: String, leftTop: Boolean = false, rightTop: Boolean = false, leftBottom: Boolean = false, rightBottom: Boolean = false, radius: Float = 12F,@DrawableRes resId: Int = R.drawable.ic_default_img ) {
    var lt = if (leftTop) radius else 0f
    var rt = if (rightTop) radius else 0f
    var lb = if (leftBottom) radius else 0f
    var rb = if (rightBottom) radius else 0f
    val transform = GranularRoundedCorners(lt,rt,rb,lb)
    val placeholder = drawable?:ContextCompat.getDrawable(context,resId)
    Glide.with(this.context).load(url)
        .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),transform)))
        .placeholder(placeholder)
        .thumbnail(0.2F)
        .into(this)
}


// 加载图片、指定圆角
fun ImageView.loadRoundUrl(url: String?, lt: Int = 0,rt: Int = 0,lb: Int = 0,rb: Int = 0,placeholder :Int = -1) {
    val transform = GranularRoundedCorners(lt.toFloat(),rt.toFloat(),rb.toFloat(),lb.toFloat())
    if (placeholder != -1 && (url == null || url.isEmpty())){
        Glide.with(this.context).load(placeholder)
            .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),transform)))
            .thumbnail(0.2F)
            .into(this)
        return
    }
    val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
    Glide.with(this.context).load(url)
        .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),transform)))
        .placeholder(placeholder)
        .thumbnail(0.2F)
        .into(this)
}

// 加载图片、指定圆角
fun ImageView.loadRoundUrl(@DrawableRes resId: Int, leftTop: Boolean = false, rightTop: Boolean = false, leftBottom: Boolean = false, rightBottom: Boolean = false, radius: Float = 12F) {
     var lt = if (leftTop) radius else 0f
    var rt = if (rightTop) radius else 0f
    var lb = if (leftBottom) radius else 0f
    var rb = if (rightBottom) radius else 0f
    val transform = GranularRoundedCorners(lt,rt,lb,rb)
    val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
    Glide.with(this.context).load(resId)
        .apply(RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(),transform)))
        .placeholder(placeholder)
        .thumbnail(0.2F)
        .into(this)
}

// 加载图片、指定圆角且毛玻璃效果
fun ImageView.loadBlurRoundUrl(url: String, leftTop: Boolean = false, rightTop: Boolean = false, leftBottom: Boolean = false, rightBottom: Boolean = false, radius: Float = 12F, blurRadius:Int = 50) {
    var lt = if (leftTop) radius else 0f
    var rt = if (rightTop) radius else 0f
    var lb = if (leftBottom) radius else 0f
    var rb = if (rightBottom) radius else 0f
    val transform = GranularRoundedCorners(lt,rt,lb,rb)
    val multi = MultiTransformation(CenterCrop(),BlurTransformation(blurRadius), transform)

    val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
    Glide.with(this.context).load(url).
    apply(RequestOptions.bitmapTransform(multi))
        .placeholder(placeholder)
        .thumbnail(0.2F)
        .into(this)
}

// 加载图片、指定圆角
fun ImageView.loadBlurRoundUrl(@DrawableRes resId: Int, leftTop: Boolean = false, rightTop: Boolean = false, leftBottom: Boolean = false, rightBottom: Boolean = false, radius: Float = 12F, blurRadius:Int = 50) {
    var lt = if (leftTop) radius else 0f
    var rt = if (rightTop) radius else 0f
    var lb = if (leftBottom) radius else 0f
    var rb = if (rightBottom) radius else 0f
    val transform = GranularRoundedCorners(lt,rt,lb,rb)
    val multi = MultiTransformation(CenterCrop(),BlurTransformation(blurRadius), transform)

    val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
    Glide.with(this.context).load(resId)
        .apply(RequestOptions.bitmapTransform(multi))
        .placeholder(placeholder)
        .thumbnail(0.2F)
        .into(this)
}

// 加载图片、原图且毛玻璃效果
fun ImageView.loadBlurCircleUrl(url: String,blurRadius:Int = 50) {
    val multi = MultiTransformation(CircleCrop(),BlurTransformation(blurRadius))

    val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
    Glide.with(this.context).load(url).
    apply(RequestOptions.bitmapTransform(multi))
        .placeholder(placeholder)
        .thumbnail(0.2F)
        .into(this)
}

// 加载图片、圆图
fun ImageView.loadBlurCircleUrl(@DrawableRes resId: Int, blurRadius:Int = 50) {
    val multi = MultiTransformation(CircleCrop(),BlurTransformation(blurRadius))
    val placeholder = drawable?:ContextCompat.getDrawable(context,R.drawable.ic_default_img)
    Glide.with(this.context).load(resId)
        .apply(RequestOptions.bitmapTransform(multi))
        .placeholder(placeholder)
        .thumbnail(0.2F)
        .into(this)
}
